/**
 * menu list
 */
layui.use(['form', 'table', 'treetable'], function () {
    var $ = layui.jquery,
        form = layui.form,
        treeTable = layui.treetable,
        table = layui.table;
    let searchFieldsetStatus=localStorage.getItem("searchFieldsetStatus");
    if(searchFieldsetStatus=='0')
    {
        $(".search-display-btn").attr("show","0");
        $(".search-display-btn").html('<i class="fa fa-angle-down"></i>展开');
        $($('.layui-form')[0]).hide();
    }
    setTableHeight();
    let u = localStorage.getItem("ADMIN_LOGIN");
    let loginU = {};
    if (u == undefined) {
        layer.msg('请登录', {icon: 1, time: 500}, function () {
            window.location = 'logout';
        });
    } else {
        loginU = decryptLocalStorage(u);
    }
    loadTree();

    function loadTree() {
        let tableUrl = baseUrl + "admin/v1/menu/page?token=" + loginU.token;
        treeTable.render({
            elem: '#currentTableId',
            url: tableUrl,loginUserType:loginU.userType,
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'menuId',
            treePidName: 'parentId',
            toolbar: '#toolbarPanel',
            page: false,
            cols: [[
                {type: "numbers", align: "center"},
                {field: 'title', align: "center", title: '菜单', sort: true},
                {field: 'icon', align: "center", title: '图标', sort: true},
                {field: 'href', align: "center", title: '链接', sort: true},
                {field: 'target', align: "center", title: '目标', sort: true},
                {fixed: "right",title: '操作', minWidth: 220, toolbar: '#currentTableBar', align: "center"}
            ]],
            done: function (res, curr, count) {
                if (res.code === -1) {
                    layer.msg('请登录', {icon: 1, time: 500}, function () {
                        window.location = 'logout';
                    });
                    return;
                }
            }
        });
    }

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'expand') {
            treeTable.expandAll('#currentTableId');
        } else if (obj.event === 'fold') {
            treeTable.foldAll('#currentTableId');
        } else if (obj.event === 'add') {  // 监听添加操作
            localStorage.removeItem('menuData');
            var index = layer.open({
                title: '添加菜单',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: false,
                area: ['80%', '80%'],
                content: '../page/menu/edit',
                end: function () {
                    setHeight();
                    loadTree();
                }

            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            let params = {token: loginU.token, ids: ''};
            let ids = '';
            if (data.length > 0) {
                for (let i = 0; i < data.length; i++) {
                    ids += data[i].menuId + ',';
                }
                ids = ids.substring(0, ids.length - 1);
            }
            params.ids = ids;
            layer.confirm("确认删除菜单，请谨慎操作？", {
                btn: ["确定", "取消"]
            }, function (index) {
                layer.close(index);
                request.delList('menu', params, function (result) {
                    if (result.code == 0) {
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 500
                        }, function () {
                            setTableHeight();
                            loadTree();
                        });
                    } else {
                        layer.msg('保存失败', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });


            }, function () {
                layer.close(index);
            });


        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        //console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            localStorage.setItem("menuData", encryptLocalStorage(data, loginU.token));
            var index = layer.open({
                title: '编辑菜单',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: false,
                area: ['80%', '80%'],
                content: '../page/menu/edit',
                end: function () { setHeight();
                    loadTree();
                }

            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm("确认删除操作？", {
                btn: ["确定", "取消"]
            }, function (index) {
                layer.close(index);
                request.del('menu', {token: loginU.token, id: obj.data.menuId}, function (result) {
                    if (result.code == 0) {
                        layer.msg('删除成功', {
                            icon: 1,
                            time: 500
                        }, function () {
                            obj.del();
                        });
                    } else {
                        layer.msg('保存失败', {
                            icon: 2,
                            time: 2000
                        });
                    }
                });
            }, function () {
                layer.close(index);
            });


        }
    });

});

function setHeight()
{
    setTimeout(function(){
        let windowHeight=$(window).height();
        let tableFieldset=$(".table-search-fieldset").height();
        if(tableFieldset=='' || tableFieldset==undefined)
        {
            tableFieldset=0;
        }
        $(".layui-table-body").height(windowHeight-tableFieldset-130);

        $(window).resize(function() {
            let windowHeight=$(window).height();
            let tableFieldset=$(".table-search-fieldset").height();
            if(tableFieldset=='' || tableFieldset==undefined)
            {
                tableFieldset=0;
            }
            $(".layui-table-body").height(windowHeight-tableFieldset-130);
        });
    },300);
}
